package com.liushao.controller;


import com.liushao.entity.vo.BorrowerVO;
import com.liushao.result.Result;
import com.liushao.service.BorrowerService;
import com.liushao.util.JwtUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author chen
 * @since 2025-01-03
 */
@RestController
@RequestMapping("/api/core/borrower")
public class BorrowerController {

    @Autowired
    private BorrowerService borrowerService;


    // /api/core/borrower/auth/save
    @ApiOperation("保存借款人信息")
    @PostMapping("/auth/save")
    public Result save(@RequestBody BorrowerVO borrowerVO, HttpServletRequest request){
        //我们要做操作  是否需要验证一下这个用户是否登录了??
        
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }        Long userId = JwtUtils.getUserId(token);
        //判断 userId是否为空  如果为空   没有登录 拦截他  让他去登录

        borrowerService.saveBorrowerVo(borrowerVO,userId);

        return  Result.ok().message("信息保存成功");
    }


    ///api/core/borrower/auth/getBorrowerStatus
    @ApiOperation("获取借款人认证状态")
    @GetMapping("/auth/getBorrowerStatus")
    public Result getBorrowerStatus(HttpServletRequest request){
        //我们要做操作  是否需要验证一下这个用户是否登录了??
        
        String token = null;
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies){
            if(cookie.getName().equals("token")){
                token = cookie.getValue();
                break;
            }
        }        Long userId = JwtUtils.getUserId(token);
        //判断 userId是否为空  如果为空   没有登录 拦截他  让他去登录

       Integer status= borrowerService.getBorrowerStatusByUserId(userId);

       return Result.ok().data("borrowerStatus",status);

    }



}

